using System.Data;
using System.Data.SqlClient;
using Provausio.Common.Data;
using Provausio.Common.Objects;
using Provausio.Common.Utility.Extensions;

namespace TrainingManagement.api.Models.Data.CRUD.Employee
{
    public class EmployeeInsertDataSource : NonQuerySource<BusinessObjects.Employee>
    {
        public override bool Execute(BusinessObjects.Employee request)
        {
            const string employeeInsert = "dbo.training_api_EmployeeInsert";

            var parameters = new SqlParameter[8];
            parameters[0] = new SqlParameter("@FirstName", SqlDbType.NVarChar) {Value = request.FirstName};
            parameters[1] = new SqlParameter("@LastName", SqlDbType.NVarChar) {Value = request.LastName};
            parameters[2] = new SqlParameter("@Email", SqlDbType.NVarChar) {Value = request.Email};
            parameters[3] = new SqlParameter("@SmsNumber", SqlDbType.NVarChar){Value = request.SmsNumber};
            parameters[4] = new SqlParameter("@Password", SqlDbType.NVarChar){Value = request.FirstName[0] + request.LastName + "1234"};
            parameters[5] = new SqlParameter("@DepartmentTreeID", SqlDbType.NVarChar){Value = request.DepartmentTreeId};
            parameters[6] = new SqlParameter("@ReportsToID", SqlDbType.Int){ Value = request.ReportsToId };
            parameters[7] = new SqlParameter("@EmployeeID", SqlDbType.Int){ Direction = ParameterDirection.Output};

            var result = SqlHelper.ExecuteNonQuery(
                    ConnectionStrings.TrainingDb,
                    CommandType.StoredProcedure,
                    employeeInsert,
                    parameters) != 0;
                request.EmployeeId = parameters[7].Value.DbTo<int>();
                return result;
        }
    }
}